package util;

import android.app.ActivityManager;
import android.content.Context;

import com.laxcen.toydemo.BaseActivity;

import java.util.Iterator;
import java.util.Stack;

/**
 * ####Activity###：##Activity#########
 *
 * @author kymjs
 * @version 1.0
 * @created 2013-11-24
 */
public class AppActivityManager {
    private static Stack<BaseActivity> activityStack = new Stack<>();

    private AppActivityManager() {
    }

    /**
     * ### , UI###########
     */
    public static AppActivityManager getInstance() {
        return AppManagerHolder.INSTANCE;
    }

    public Stack<BaseActivity> getActivities() {
        return activityStack;
    }

    /**
     * ##Activity##
     */
    public void addActivity(BaseActivity activity) {
        activityStack.add(activity);
    }

    /**
     * ####Activity（##Activity）
     */
    public BaseActivity currentActivity() {
        if (activityStack == null || activityStack.isEmpty()) {
            return null;
        }
        BaseActivity activity = activityStack.lastElement();
        return activity;
    }

    public BaseActivity preActivity() {
        if (activityStack == null || activityStack.isEmpty() || activityStack.size() <= 1) {
            return null;
        }
        BaseActivity activity = activityStack.get(1);
        return activity;
    }

    /**
     * ####Activity（##Activity） #######null
     */
    public BaseActivity findActivity(Class<?> cls) {
        BaseActivity activity = null;
        for (BaseActivity aty : activityStack) {
            if (aty.getClass().equals(cls)) {
                activity = aty;
                break;
            }
        }
        return activity;
    }

    /**
     * ####Activity（##Activity）
     */
    public void finishActivity() {
        BaseActivity activity = activityStack.lastElement();
        finishActivity(activity);
    }

    /**
     * #####Activity(##)
     */
    public void finishActivity(BaseActivity activity) {
        if (activity != null) {
            activityStack.remove(activity);
            activity.finish();
            activity = null;
        }
    }

    /**
     * #####Activity(##)
     */
    public void finishActivity(Class<?> cls) {
        BaseActivity findBaseActivity = null;
        for (BaseActivity activity : activityStack) {
            if (activity.getClass().equals(cls)) {
                findBaseActivity = activity;
            }
        }
        finishActivity(findBaseActivity);
    }

    /**
     * ######activity#####activity ##cls######，######
     *
     * @param cls
     */
    public void finishOtherActivities(Class<?> cls) {
        Iterator<BaseActivity> activityIterator = activityStack.iterator();
        while (activityIterator.hasNext()) {
            BaseActivity activity = activityIterator.next();
            if (!(activity.getClass().equals(cls))) {
                activityIterator.remove();
                activity = null;
            }
        }
    }

    /**
     * ######activity#####activity ##act######，######
     *
     * @param act
     */
    public void finishOtherActivities(BaseActivity act) {
        Iterator<BaseActivity> activityIterator = activityStack.iterator();
        while (activityIterator.hasNext()) {
            BaseActivity activity = activityIterator.next();
            if (activity != act) {
                activity.finish();
                activityIterator.remove();
                activity = null;
            }
        }
    }

    /**
     * ####MessageDialogActivity###Activity
     */
    public void finishAllActivityButMsg() {
        for (BaseActivity activity : activityStack) {
            if (null != activity) {
                activity.finish();
            }
        }
        activityStack.clear();
    }

    /**
     * ####Activity
     */
    public void finishAllActivity() {
        for (BaseActivity activity : activityStack) {
            if (null != activity) {
                activity.finish();
            }
        }
        activityStack.clear();
    }

    /**
     * ####activity
     */
//    public void refreshActivity(Class<? extends IRefreshable> cls) {
//        for (BaseActivity activity : activityStack) {
//            if (activity.getClass().getName().equals(cls.getName())) {
//                ((IRefreshable) activity).refresh();
//                LogUtil.d("refreshActivity Activity:" + activity.getClass().getName());
//            }
//        }
//    }

    /**
     * ######
     */
//    public void AppExit(Context context) {
//        try {
//            finishAllActivity();
//            ActivityManager activityMgr = (ActivityManager) context
//                    .getSystemService(Context.ACTIVITY_SERVICE);
//            activityMgr.killBackgroundProcesses(context.getPackageName());
//            System.exit(0);
//        } catch (Exception e) {
//            System.exit(0);
//        }
//    }

    private static class AppManagerHolder {
        private static final AppActivityManager INSTANCE = new AppActivityManager();
    }



}